#ifndef __CHAOSPROFILEMANAGER_HPP
#define	__CHAOSPROFILEMANAGER_HPP

#include "ChaosGlobalDefine.hpp"
#include "ChaosSingleton.hpp"

#include "ChaosProfileNode.hpp"

namespace Chaos
{
	/*
	** An iterator to navigate through the tree
	*/
	class CHAOS_API ChsProfileIterator
	{
	public:
		// Access all the children of the current parent
		void				First(void);
		void				Next(void);
		bool				IsDone(void)const;

		void				EnterChild(const int index );		// Make the given child the new parent
		void				EnterLargest_Child( void );			// Make the largest child the new parent
		void				EnterParent( void );				// Make the current parent's parent the new parent

		// Access the current child
		const TCHAR *		GetCurrentName( void )const;
		int					GetCurrentTotalCalls( void )const;
		float				GetCurrentTotalTime( void )const;
		int					GetCurrentLastTotalCalls( void )const;
		float				GetCurrentLastTotalTime( void )const;

		// Access the current parent
		const TCHAR *		GetCurrentParentName( void )const;
		int					GetCurrentParentTotalCalls( void )const;
		float				GetCurrentParentTotalTime( void )const;
		int					GetCurrentParentLastTotalCalls( void )const;
		float				GetCurrentParentLastTotalTime( void )const;

	protected:

		ChsProfileNode *	m_pCurrentParent;
		ChsProfileNode *	m_pCurrentChild;

							ChsProfileIterator( ChsProfileNode * start );
		friend	class		ChsProfileManager;
	};

	inline const TCHAR * ChsProfileIterator::GetCurrentName( void )const
	{
		return m_pCurrentChild->GetName();
	}

	inline int ChsProfileIterator::GetCurrentTotalCalls( void )const
	{
		return m_pCurrentChild->GetTotalCalls();
	}

	inline float ChsProfileIterator::GetCurrentTotalTime( void )const
	{
		return m_pCurrentChild->GetTotalTime();
	}

	inline int	ChsProfileIterator::GetCurrentLastTotalCalls( void )const
	{
		return m_pCurrentChild->GetLastTotalCalls();
	}

	inline float ChsProfileIterator::GetCurrentLastTotalTime( void )const
	{
		return m_pCurrentChild->GetLastTotalTime();
	}


	class CHAOS_API ChsProfileManager : public ChsSingleton<ChsProfileManager>
	{
	private:
			ChsProfileNode			m_Root;
			int						m_nFrameCounter;
			__int64					m_n64ResetTime;
			ChsProfileNode*			m_pCurrentNode;
	public:
									ChsProfileManager();
									~ChsProfileManager();

		void						Reset( void );
		void						IncrementFrameCounter( void );
		int							GetFrameCountSinceReset( void )const;
		float						GetTimeSinceReset( void );

		ChsProfileIterator*			GetIterator( void );
		void						ReleaseIterator( ChsProfileIterator * iterator );

		void						StartProfile(const TCHAR *name);
		void						StopProfile();
	};


	inline ChsProfileIterator* ChsProfileManager::GetIterator( void )
	{
		return new ChsProfileIterator( &m_Root );
	}

	inline void ChsProfileManager::ReleaseIterator( ChsProfileIterator * iterator )
	{
		delete iterator;
	}

	inline int ChsProfileManager::GetFrameCountSinceReset( void )const
	{
		return m_nFrameCounter; 
	}
}






#endif	//__CHAOSPROFILEMANAGER_HPP
